<template>
    <div>
        <h3>科室添加</h3>
        {{ info }}
        <table>
            <thead>
                <tr>
                    <td>姓名</td>
                    <td><input type="text" v-model="info.dName" ></td>
                </tr>

                <tr>
                    <td>头像</td>
                    <td><input type="file" @change="img" >
                        <img :src="info.img" width="60px" height="60px"  ></td>
                </tr>

                <tr>
                    <td>所属科室</td>
                    <td><select v-model="info.kid">
                        <option value="">请选择</option>
                        <option :value="o.kid" v-for="o in Ken">{{ o.kName }}</option>
                    </select></td>
                </tr>

                <tr>
                    <td>创建日期</td>
                    <td><input type="date" v-model="info.ddate"></td>
                </tr>
                
                <tr>
                    <td>所属医院</td>
                    <td><input type="text" v-model="info.add" ></td>
                </tr>
                
                <tr>
                    <td></td>
                    <td><input type="button" value="添加" @click="Addd"></td>
                </tr>
            </thead>
        </table>
    </div>
</template>

<script setup lang="ts">
import axios from 'axios';
import { onMounted, ref } from 'vue';
import { useRouter } from 'vue-router';
const router=useRouter();

const Tifand=()=>{
    router.push({
        path:"/showD",
    })
}

const img=(e:any)=>{
    console.log(e);
    
    var f=e.target.files[0];

    var ds=new FormData();
    ds.append("file",f );

    axios({
        url:"https://localhost:7030/api/Img",
        method:'post',
        data:ds
    })
    .then((res)=>{
        console.log(res);
        info.value.img=res.data
    })
}


onMounted(()=>{
    List();
})

let info:any=ref({
  "dName": "",
  "img": "",
  "kid": "",
  "ddate": "",
  "add": "",
  "isDell": 0
});

const Addd=()=>{
    if(info.value.dName=="" )
{
    alert("姓名不为空");
    return;
}
    
    if(info.value.kid=="" )
{
    alert("所属科室不为空");
    return;
}
    if(info.value.ddate=="" )
{
    alert("创建日期不为空");
    return;
} 
    if(info.value.add=="" )
{
    alert("所属医院不为空");
    return;
}
    axios({
        url:"https://localhost:7030/api/User/AddD",
        method:'post',
        data:info.value
    })
    .then((res)=>{
        console.log(res);
        if(res.data==-1 )
    {
        alert("姓名存在");
        return;
    }
        if(res.data>0 )
    {
        alert("添加成功");
        Tifand();
    }
    })


}

let Ken:any=ref([]);

const List=()=>{
    axios({
        url:"https://localhost:7030/api/User/ListK",
        method:'get',
    })
    .then((res)=>{
        console.log(res);
        Ken.value=res.data;
    })
}



</script>

<style scoped>

</style>